package com.sharekitchen.service.impl;

import com.sharekitchen.entity.Admin;
import com.sharekitchen.entity.User;
import com.sharekitchen.mapper.AdminMapper;
import com.sharekitchen.mapper.UserMapper;
import com.sharekitchen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    //    校验用户是否存在
    @Override
    public boolean existUsername(String username) {
//        也可以改变mapper返回 int查询多少条
//        如果这个用户能够找到信息,说明已存在,则返回true
        return userMapper.existUsername(username) != null;
    }

    //    添加用户
    @Override
    public boolean addUser(User user) {
        LocalDate currentDate = LocalDate.now();
        // 将日期格式化为指定格式的字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        user.setCreateTime(currentDate.format(formatter));
        return userMapper.addUser(user) > 0;
    }

    //    获取用户信息
    @Override
    public User findUserByName(String username) {
        return userMapper.findUserByName(username);
    }

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }


    @Override
    public boolean deleteUserByName(String username) {
        return userMapper.deleteUserByName(username);
    }
}
